import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request
import { AuthSiteService } from '../../../auth/services/admin/auth-site.service';
import { SiteService } from '../../services/admin/site.service';
import { CaptchaService } from '../../../captcha/services/api/captcha.service';

/**
 * SiteController
 * 对应 PHP: Site Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('site')
@Controller('adminapi/site')
export class SiteController {
  constructor(
    private readonly authSiteService: AuthSiteService,
    private readonly siteService: SiteService,
    private readonly captchaService: CaptchaService,
  ) {}

  /**
   * 站点管理
   * 路由: GET site
   * PHP路由: Route::get('site', 'site.Site/lists')
   */
  @Get('site')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.siteService.getPage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET site/:id
   * PHP路由: Route::get('site/:id', 'site.Site/info')
   */
  @Get('site/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async info(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.siteService.getInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: POST site
   * PHP路由: Route::post('site', 'site.Site/add')
   */
  @Post('site')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.siteService.add(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET statuslist
   * PHP路由: Route::get('statuslist', 'site.Site/getStatuList')
   */
  @Get('statuslist')
  @ApiOperation({ summary: '站点管理' })
  async getStatuList() {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getStatuList
      // 直接返回字典数据
      return SiteDict.getStatus();
    } catch (error) {
      throw new BusinessException('getStatuList操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET site/menu
   * PHP路由: Route::get('site/menu', 'site.Site/menu')
   */
  @Get('site/menu')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async menu(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: menu

      return await this.authSiteService.getMenuList(1, 1, 'all', 0);
    } catch (error) {
      throw new BusinessException('menu操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: PUT closesite/:id
   * PHP路由: Route::put('closesite/:id', 'site.Site/closeSite')
   */
  @Put('closesite/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async closeSite(
    @Param('id') id: string,
    @Body() data: CloseSiteDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: closeSite

      return await this.siteService.edit(id, data);
    } catch (error) {
      throw new BusinessException('closeSite操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: PUT site/:id
   * PHP路由: Route::put('site/:id', 'site.Site/edit')
   */
  @Put('site/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.siteService.edit(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: DELETE site/:id
   * PHP路由: Route::delete('site/:id', 'site.Site/del')
   */
  @Delete('site/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async del(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.siteService.del(id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: PUT opensite/:id
   * PHP路由: Route::put('opensite/:id', 'site.Site/openSite')
   */
  @Put('opensite/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async openSite(
    @Param('id') id: string,
    @Body() data: OpenSiteDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: openSite

      return await this.siteService.edit(id, data);
    } catch (error) {
      throw new BusinessException('openSite操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET addons
   * PHP路由: Route::get('addons', 'site.Site/addons')
   */
  @Get('addons')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async addons(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: addons

      return await this.siteService.getSiteAddons(data);
    } catch (error) {
      throw new BusinessException('addons操作失败', error);
    }
  }

  /**
   * showApp
   * 路由: GET showapp
   * PHP路由: Route::get('showapp', 'site.Site/showApp')
   */
  @Get('showapp')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'showApp' })
  async showApp(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: showApp

      return await this.siteService.getShowAppTools();
    } catch (error) {
      throw new BusinessException('showApp操作失败', error);
    }
  }

  /**
   * showMarketing
   * 路由: GET showMarketing
   * PHP路由: Route::get('showMarketing', 'site.Site/showMarketing')
   */
  @Get('showMarketing')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: 'showMarketing' })
  async showMarketing(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: showMarketing

      return await this.siteService.getShowMarketingTools();
    } catch (error) {
      throw new BusinessException('showMarketing操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: POST init
   * PHP路由: Route::post('init', 'site.Site/siteInit')
   */
  @Post('init')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async siteInit(@Body() data: SiteInitDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: siteInit

      return await this.siteService.siteInit(data);
    } catch (error) {
      throw new BusinessException('siteInit操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: PUT allow_change
   * PHP路由: Route::put('allow_change', 'site.Site/setIsAllowChangeSite')
   */
  @Put('allow_change')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async setIsAllowChangeSite(
    @Body() data: SetIsAllowChangeSiteDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: setIsAllowChangeSite

      return await this.siteService.setIsAllowChangeSite(data['is_allow']);
    } catch (error) {
      throw new BusinessException('setIsAllowChangeSite操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET allow_change
   * PHP路由: Route::get('allow_change', 'site.Site/getIsAllowChangeSite')
   */
  @Get('allow_change')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async getIsAllowChangeSite(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getIsAllowChangeSite

      return await this.siteService.getIsAllowChangeSite();
    } catch (error) {
      throw new BusinessException('getIsAllowChangeSite操作失败', error);
    }
  }

  /**
   * 站点管理
   * 路由: GET captcha/create
   * PHP路由: Route::get('captcha/create', 'site.Site/captcha')
   */
  @Get('captcha/create')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '站点管理' })
  async captcha(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: captcha

      return await this.captchaService.create();
    } catch (error) {
      throw new BusinessException('captcha操作失败', error);
    }
  }
}
